<template>
  <a-modal
    :title="model && model.id > 0 ? '合量': '新增合量'"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="() => { $emit('ok') }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning="loading">
      <a-form :form="form" v-bind="formLayout">
        <a-form-item label="合量名称">
          <a-input v-decorator="['userName']" placeholder="请输入合量名称" name="userName"/>
        </a-form-item>
        <a-form-item label="入职日期">
          <a-date-picker v-decorator="['inDate']" style="width: 100%" name="inDate" valueFormat="YYYY-MM-DD"/>
        </a-form-item>
        <a-form-item label="归属店">
          <a-select v-decorator="['storeId']" placeholder="请选择" name="storeId" >
            <a-select-option value="1">达甄贸易王升</a-select-option>
            <a-select-option value="2">达甄东街店</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="合量列表">
          <a-select v-decorator="['empType']" placeholder="请选择" name="empType" >
            <a-select-option value="1">王生</a-select-option>
            <a-select-option value="2">黄东亮</a-select-option>
            <a-select-option value="3">测试</a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
    
<script>
import pick from 'lodash.pick'
// 表单字段
const fields = ['description', 'id']

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  data () {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this)
    }
  },
  created () {
    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单设置值
    this.$watch('model', () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields))
    })
  }
}
</script>